package com.sihai.ehr.web.action.employee;

import com.opensymphony.xwork2.ActionSupport;
import com.sihai.core.beans.Property;
import com.sihai.ehr.utils.StringUtil;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.action.ajax.AjaxCallback;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUploadAction extends ActionSupport {
    private final String root_path = "upload";

    @Action(value = "/file/upload", interceptorRefs = {@InterceptorRef(value = "fileUpload"), @InterceptorRef("ehr-interceptor")})
    public String upload() throws Exception {
        if (this.uploads == null || this.uploads.length == 0) {
            AjaxCallback.failure().flush(ServletActionContext.getResponse());
        }
        String webRootPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/");
        File f = new File(String.format("%s/%s", webRootPath, root_path));
        System.out.println("wxn---web root path = " + f.getAbsolutePath());
        if (!f.exists()) {
            boolean b = f.mkdir();
            //TODO:if(!b){...}
        }

        if (uploads != null && uploads.length > 0) {
            String filePath = null;
            for (int i = 0; i < uploads.length; i++) {
                File upload = uploads[i];
                String realName = uploadsFileName[i];
                //String contentType = uploadsContentType[i];
                String destFilePath = String.format("%s/%s_%s_%s", DateFormatUtils.format(new Date(), "yyyy-MM-dd"), System.currentTimeMillis(), RandomStringUtils.randomAlphanumeric(4), realName);
                File destFile = new File(String.format("%s/%s", f.getAbsolutePath(), destFilePath));
                FileUtils.copyFile(upload, destFile);
                //如果多文件上传,用StringBuilder
                filePath = destFilePath;
            }
            System.out.println("======" + filePath);
            AjaxCallback.success("filePath", String.format("/%s/%s", root_path, filePath)).flush(ServletActionContext.getResponse());
        }

        return NONE;
    }
    
    @Action(value = "/file/upload_resource", interceptorRefs = {@InterceptorRef(value = "fileUpload"), @InterceptorRef("ehr-interceptor")})
    public String uploadMaterResource() throws Exception {
        if (this.uploads == null || this.uploads.length == 0) {
            AjaxCallback.failure().flush(ServletActionContext.getResponse());
        }
        String webRootPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/");
        File f = new File(String.format("%s/%s", webRootPath, root_path));
        System.out.println("wxn---web root path = " + f.getAbsolutePath());
        if (!f.exists()) {
            boolean b = f.mkdir();
            //TODO:if(!b){...}
        }

        if (uploads != null && uploads.length > 0) {
            String filePath = null;
            String realName = null;
            for (int i = 0; i < uploads.length; i++) {
                File upload = uploads[i];
                realName = uploadsFileName[i];
                String serverFileName = realName.indexOf(".") > -1 ? realName.substring(realName.indexOf(".")) : "";
                //String contentType = uploadsContentType[i];
                String destFilePath = String.format("%s/%s_%s_%s", DateFormatUtils.format(new Date(), "yyyy-MM-dd"), System.currentTimeMillis(), RandomStringUtils.randomAlphanumeric(4), serverFileName);
                File destFile = new File(String.format("%s/%s", f.getAbsolutePath(), destFilePath));
                FileUtils.copyFile(upload, destFile);
                //如果多文件上传,用StringBuilder
                filePath = destFilePath;
            }
            System.out.println("======" + filePath);
            AjaxCallback.success("filePath", String.format("/%s/%s", root_path, filePath) , StringUtil.escape(realName)).flush(ServletActionContext.getResponse());
        }

        return NONE;
    }

    @Action(value = "/upload_test", results = {@Result(
            name = "test", location = "upload_test.jsp"
    )})
    public String test() throws Exception {
        return "test";
    }

    private File[] uploads;

    private String[] uploadsFileName;

    private String[] uploadsContentType;

    public void setUploads(File[] uploads) {
        this.uploads = uploads;
    }

    public void setUploadsFileName(String[] uploadsFileName) {
        this.uploadsFileName = uploadsFileName;
    }

    public void setUploadsContentType(String[] uploadsContentType) {
        this.uploadsContentType = uploadsContentType;
    }
}
